package com.action;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.pojo.Module;
import com.pojo.Newtype;
import com.service.NewtypeService;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/newtype")
public class NewtypeAction {
    @Autowired
    private NewtypeService newtypeService;

    @Value("${upload.dir}")
    private String upload ;

    @Value("${upload.tmp.dir}")
    private String tmp ;


    /**
     * 分页查询
     * @param page
     * @param limit
     * @param key
     * @return
     */
    @RequestMapping("/types")
    public ResponseBean types(@RequestParam(defaultValue = "1")Integer page,
                              @RequestParam(defaultValue = "10")Integer limit,
                              @RequestParam(required = false) String key,Integer status){
        if(StringUtil.isNull(key)){
            key =null;
        }
        IPage iPage = newtypeService.types(page,limit,key,status);
        return new ResponseBean(0,iPage.getTotal(),"ok",iPage.getRecords());
    }


    /**
     * 修改通知状态
     */
    @RequestMapping("/upStatus")
    public ResponseBean changeStatus(String id,Integer status) {
        Newtype topic = new Newtype();
        topic.setId(id);
        topic.setStatus(status);
        if(newtypeService.updateById(topic)){
            return new ResponseBean(0,1,"修改状态成功");
        }
        return new ResponseBean(1,1,"修改状态失败");
    }

    /**
     * 新增新闻类名
     * @param newtype
     * @return
     */
    @RequestMapping("/addnewtype")
    public ResponseBean addnewtype(@RequestBody Newtype newtype){
        if(StringUtil.isNull(newtype.getXwimg())){
            return ResponseBean.error("请先上传图片");
        }
        if(StringUtil.isNull(newtype.getName())){
            return ResponseBean.error("请填写名称");
        }
        if(newtype.getStatus() == null) {
            return ResponseBean.error("请选择状态") ;
        }
        Newtype n = newtypeService.getOne(new QueryWrapper<Newtype>().eq("name",newtype.getName()).last("limit 1"));
        if(n != null){
            return ResponseBean.error("名字已经存在");
        }
        boolean res = newtypeService.save(newtype);
        FileUtil.copy(tmp + newtype.getXwimg(), upload,true);
        return res ? new ResponseBean(200,"成功",""): ResponseBean.error("新增失败");
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping("/getnewtypeById")
    public ResponseBean getnewtypeById(String id){
        Newtype newtype = newtypeService.getById(id);
        if(newtype != null){
            return new ResponseBean(200,"查询成功",newtype);
        }
        return ResponseBean.error("查询失败");
    }

    /**
     * 修改新闻群
     * @param newtype
     * @return
     */
    @RequestMapping("/updnewtype")
    public ResponseBean updnewtype(@RequestBody Newtype newtype){
        if(StringUtil.isNull(newtype.getId())){
            return ResponseBean.error("id不能空");
        }
        if(StringUtil.isNull(newtype.getName())){
            newtype.setName(null);
        }
        if(StringUtil.isNull(newtype.getXwimg())){
            newtype.setXwimg(null);
        }
        Newtype newtypeTow = newtypeService.getById(newtype.getId());
        if(newtypeService.updateById(newtype)){
            if(!newtype.getXwimg().equals(newtypeTow.getXwimg())){
                FileUtil.copy(tmp + newtype.getXwimg(),upload ,true);
            }
            return ResponseBean.ok("操作成功");
        }
        return ResponseBean.error("操作失败");
    }

    /**
     * 查询所有新闻分类
     * @return
     */
    @RequestMapping("/newtypeAll")
    public ResponseBean newtypeAll(Integer status){
        QueryWrapper queryWrapper = new QueryWrapper<Newtype>();
        if(status != null){
            queryWrapper.eq("status",status);
        }
        List<Newtype> iPage = newtypeService.list(queryWrapper);
        return new ResponseBean(0,"ok",iPage);
    }

}
